import request from '@/utils/request'

export const getNewsPhotos = (params) => request.get('/api/news-photos', { params })
export const searchNewsPhotos = (params) => request.get('/api/news-photos/search', { params })
export const getNewsPhotoDetail = (id) => request.get(`/api/news-photos/${id}`)
export const createNewsPhoto = (data, operatorId) =>
  request.post('/api/news-photos', data, {
    params: {
      operatorId
    }
  });
export const updateNewsPhoto = (id, data) => request.put(`/api/news-photos/${id}`, data)
export const batchUpdateArchiveStatus = (params) => request.put('/api/news-photos/batch-archive', null, {
  params
})
export const batchMoveArchive = (params) => request.put('/api/news-photos/batch-move-archive', null,{
  params
})
export const deleteNewsPhoto = (id) => request.delete(`/api/news-photos/${id}`)
export const upload = (formData) => request.post(`/api/upload/image`, formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
// 获取目录
export const getFileTree = () => request.get('/api/documents/tree')
export const downloadFile = (params) => request.get('/api/documents/download', { params })
// 导入目录照片
export const importFile = (params) => request.post('/api/news-photos/importPhoto', null, {
  params
})
export const postRenameFolder = (params) => request.post('/api/news-photos/rename-folder', null, {
  params
})
// 简化版本
export const downloadImageZip = async (params) => {
  try {
    const response = await request({
      url: '/api/news-photos/downloadZip',
      method: 'get',
      params: params,
      responseType: 'blob'
    });

    // 验证响应
    if (!response) {
      throw new Error('服务器未返回数据');
    }

    // 如果返回的是错误信息文本（如JSON格式的错误）
    if (response.type && response.type.includes('json')) {
      const errorText = await response.text();
      throw new Error(`服务器错误: ${errorText}`);
    }

    // 创建下载链接
    const url = window.URL.createObjectURL(new Blob([response]));
    const link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', '相片档案.zip');
    
    document.body.appendChild(link);
    link.click();
    
    setTimeout(() => {
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);
    }, 100);
    
    return response;
  } catch (error) {
    console.error('下载失败:', error);
    throw error;
  }
}


// 新增导入Excel接口
export const importExcel = (formData) => request.post('/api/news-photos/importExcel', formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})

// 重编档号
export const reFileNumber = (params) => request.post('/api/news-photos/genFileNumber', null, {
  params
})

export const renameDocFiles = (params) => request.post('/api/news-photos/renameDocFiles', null, {
  params
})